package com.lezhu.plugins;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.umeng.analytics.MobclickAgent;
import com.umeng.analytics.social.UMPlatformData;
import com.umeng.analytics.social.UMPlatformData.GENDER;
import com.umeng.analytics.social.UMPlatformData.UMedia;

import java.util.HashMap;
import java.util.Map;

public class UmengAnalyticsPlugin extends CordovaPlugin {

    private static String EVENT_INIT = "init";

    private static String EVENT_SET_DEBUG_MODE = "setDebugMode";

    private static String EVENT_ON_KILL_PROCESS = "onKillProcess";

    private static String EVENT_ON_PAGE_START = "onPageStart";

    private static String EVENT_ON_PAGE_END = "onPageEnd";

    private static String EVENT_ON_EVENT = "onEvent";

    private static String EVENT_ON_EVENT_VALUE = "onEventValue";

    private static final String PARA_EVENT_ID = "eventId";

    private static final String PARA_CONTENT = "content";

    private static final String PARA_VALUE = "value";


    private Context mContext;

    @Override
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);
        mContext = this.cordova.getActivity().getApplicationContext();
    }

    @Override
    public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {
        if (EVENT_INIT.equals(action)) {
            init(callbackContext);
        } else if (EVENT_SET_DEBUG_MODE.equals(action)) {
            this.setDebugMode(callbackContext, data);
        } else if (EVENT_ON_KILL_PROCESS.equals(action)) {
            onKillProcess(callbackContext);
        } else if (EVENT_ON_PAGE_START.equals(action)) {
            this.onPageStart(callbackContext, data);
        } else if (EVENT_ON_PAGE_END.equals(action)) {
            this.onPageEnd(callbackContext, data);
        } else if (EVENT_ON_EVENT.equals(action)) {
            this.onEvent(callbackContext, data);
        } else if (EVENT_ON_EVENT_VALUE.equals(action)) {
            this.onEventValue(callbackContext, data);
        } else {

        }
        return true;
    }

    private void init(CallbackContext callbackContext) {
        MobclickAgent.openActivityDurationTrack(false);
        MobclickAgent.setAutoLocation(true);
        MobclickAgent.updateOnlineConfig(mContext);
        callbackContext.success();
    }

    private void setDebugMode(CallbackContext callbackContext, JSONArray data) {
        try {
            boolean mode = data.getBoolean(0);
            MobclickAgent.setDebugMode(mode);
            callbackContext.success();
        } catch (JSONException e) {
            callbackContext.error("参数不正确");
        }
    }

    private void onKillProcess(CallbackContext callbackContext) {
        MobclickAgent.onKillProcess(mContext);
        callbackContext.success();
    }

    private void onPageStart(CallbackContext callbackContext, JSONArray data) {
        try {
            String pageName = data.getString(0);
            MobclickAgent.onPageStart(pageName);
            MobclickAgent.onResume(mContext);
        } catch (JSONException e) {
            callbackContext.error("参数不能为null");
        }
    }

    private void onPageEnd(CallbackContext callbackContext, JSONArray data) {
        try {
            String pageName = data.getString(0);
            MobclickAgent.onPageEnd(pageName);
            MobclickAgent.onPause(mContext);
        } catch (JSONException e) {
            callbackContext.error("参数不能为null");
        }
    }

    private void onEvent(CallbackContext callbackContext, JSONArray data) {
        try {
            JSONObject jsonObject = data.getJSONObject(0);
            String eventId = jsonObject.getString(PARA_EVENT_ID);

            JSONObject content = this.getJSONValueRtnNull(PARA_CONTENT, jsonObject);
            if (content == null) {
                MobclickAgent.onEvent(mContext, eventId);
            } else {
                Map<String, String> map = this.jsonToMap(content);
                MobclickAgent.onEvent(mContext, eventId, map);
            }

            callbackContext.success();
        } catch (JSONException e) {
            callbackContext.error("参数错误");
        }
    }

    private void onEventValue(CallbackContext callbackContext, JSONArray data) {
        try {
            JSONObject jsonObject = data.getJSONObject(0);
            String eventId = jsonObject.getString(PARA_EVENT_ID);

            JSONObject content = jsonObject.getJSONObject(PARA_CONTENT);
            Map<String, String> map = this.jsonToMap(content);

            int value = jsonObject.getInt(PARA_VALUE);

            MobclickAgent.onEventValue(mContext, eventId, map, value);

            callbackContext.success();
        } catch (JSONException e) {
            callbackContext.error("参数错误");
        }
    }

    private Map<String, String> jsonToMap(JSONObject content) {
        Map<String, String> map = new HashMap<String, String>();
        JSONArray names = content.names();
        int length = names.length();
        try {
            for (int i = 0; i < length; i++) {
                String name = names.getString(i);
                String value = content.getString(name);
                map.put(name, value);
            }
        } catch (JSONException e) {
        }
        return map;
    }


    private JSONObject getJSONValueRtnNull(String name, JSONObject jsonObject) {
        try {
            return jsonObject.getJSONObject(name);
        } catch (JSONException e) {
            return null;
        }
    }

}